<template>
  <basic-container>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               :page="page"
               :permission="permissionList"
               :before-open="beforeOpen"
               v-model="form"
               ref="crud"
               @row-update="rowUpdate"
               @row-save="rowSave"
               @row-del="rowDel"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @refresh-change="refreshChange"
               @on-load="onLoad">
      <template slot="menuLeft">
        <el-button type="danger"
                   size="small"
                   icon="el-icon-delete"
                   plain
                   v-if="permission.lineEvent_delete"
                   @click="handleDelete">删 除
        </el-button>
      </template>
    </avue-crud>
     <el-dialog title="选择坐标点"
      ref="gridDialog"
        append-to-body
        :visible.sync="mapBox"
        :fullscreen="false"
        style="width:100%;height:900px;">
      <coordinate-map style="height:600px;" v-on:getGridData="getGridMap($event)" :lngv="form.lng ? form.lng :''" :latv="form.lat ? form.lat :''" />
    </el-dialog>
  </basic-container>
</template>

<script>
  import {getList, getDetail, add, update, remove} from "@/api/ninebasic/wayline/lineEvent";
  import {mapGetters} from "vuex";
  import { validatenum,validatenull} from "@/util/validate";
//打点坐标组件
  import coordinateMap from '@/components/gis/coordinateMap'
  export default {
    data() {
       //校验正整数
      const validateNum = (rule, value, callback) => {
        if(!validatenull(value)){
          if(validatenum(value,1)){
            callback("请输入正整数")
          }else{
            callback();
          }
        }
      };
      return {
        mapBox: false,
        form: {},
        query: {},
        loading: true,
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        selectionList: [],
        option: {
          height: 'auto',
          calcHeight: 30,
          dialogWidth: 1150,
          tip: false,
          searchShow: true,
          searchMenuSpan: 6,
          border: true,
          index: true,
          viewBtn: true,
          selection: true,
          excelBtn: true,
          dialogClickModal: false,
          column: [
            {
              label: "所属网格",
              prop: "gridId",
              type: "tree",
              span: 24,
              //multiple: true,
              parent: true,
              display: true,
              search: true,
              dicMethod: "get",
              dicUrl: "/api/smart-system/dept/treeByPid",
              props: {
                label: "title"
              
              },
              rules: [{
                required: true,
                message: "请输入所属网格",
                trigger: "blur"
              }],
           },
            {
              label: "案（事）件编号",
              prop: "eventNo",
              type: "input",
              search: true,
            },
            {
              label: "案（事）件名称",
              prop: "eventName",
              type: "input",
              search: true,
              rules: [{
                required: true,
                message: "请输入案（事）件名称",
                trigger: "blur"
              }],
            },
            {
              label: "发生日期",
              prop: "happenDate",
              type: "date",
              format: "yyyy-MM-dd",
              valueFormat: "yyyy-MM-dd",
              rules: [{
                required: true,
                message: "请输入发生日期",
                trigger: "blur"
              }],
            },
           
            {
              label: "发案地",
              prop: "addr",
              type: "input",
              rules: [{
                required: true,
                message: "请输入发案地",
                trigger: "blur"
              }],
            },
            {
              label: "详细地址",
              prop: "addrDetail",
              type: "input",
              hide: true,
              rules: [{
                required: true,
                message: "请输入详细地址",
                trigger: "blur"
              }],
            },
            {
              label: "案（事）件性质",
              prop: "eventNature",
              type: "select",
              dicUrl: "/api/smart-system/dict/dictionary?code=case_type",
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              search: true,
              rules: [{
                required: true,
                message: "请输入案（事）件性质",
                trigger: "blur"
              }],
            },
            {
              label: "案（事）件情况",
              prop: "eventCase",
              type: "textarea",
              hide: true,
              span: 24,
              rules: [{
                required: true,
                message: "请输入案（事）件情况",
                trigger: "blur"
              }],
            },
             {
              label: "主犯(嫌疑人)姓名",
              prop: "prisonerName",
              type: "input",
              hide: true,
              rules: [{
                required: true,
                message: "请输入主犯(嫌疑人)姓名",
                trigger: "blur"
              }],
            },
            {
              label: "主犯(嫌疑人)证件代码",
              prop: "prisonerCode",
              type: "select",
              hide: true,
               dicUrl: "/api/smart-system/dict/dictionary?code=certificate_code",
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              rules: [{
                required: true,
                message: "请输入主犯(嫌疑人)证件代码",
                trigger: "blur"
              }],
            },
            {
              label: "主犯(嫌疑人)证件号码",
              prop: "prisonerNo",
              type: "input",
              hide: true,
              rules: [{
                required: true,
                message: "请输入主犯(嫌疑人)证件号码",
                trigger: "blur"
              }],
            },
           
            {
              label: "是否破案",
              prop: "isSolve",
              type: "radio",
              dicUrl: "/api/smart-system/dict/dictionary?code=yes_no",
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              search: true,
              rules: [{
                required: true,
                message: "请输入是否破案",
                trigger: "blur"
              }],
            },
            {
              label: "作案人数",
              prop: "doAmount",
              type: "input",
              hide: true,
              rules: [{validator: validateNum,}],
              span: 8,
            },
            {
              label: "在逃人数",
              prop: "fleeAmount",
              type: "input",
              hide: true,
              rules: [{validator: validateNum,}],
              span: 8,
            },
            {
              label: "抓捕人数",
              prop: "arrestAmount",
              type: "input",
              hide: true,
              rules: [{validator: validateNum,}],
              span: 8,
            },
            {
              label: "案件侦破情况",
              prop: "solveCase",
              type: "textarea",
              hide: true,
              span: 24,
              rules: [{
                required: true,
                message: "请输入案件侦破情况",
                trigger: "blur"
              }],
            },
            {
              label: "经度",
              prop: "lng",
              type: "input",
              hide: true,
              suffixIcon:'el-icon-map-location',
              props: {
                label: "lng",
                value: ""
              },
              click:()=>{
                this.openGrid()
              }
            },
            {
              label: "纬度",
              prop: "lat",
              type: "input",
              hide: true,
              suffixIcon:'el-icon-map-location',
              props: {
                label: "lat",
                value: ""
              },
              click:()=>{
                this.openGrid()
              }
            },
            
          ]
        },
        data: []
      };
    },
    components:{
      coordinateMap
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: this.vaildData(this.permission.lineEvent_add, false),
          viewBtn: this.vaildData(this.permission.lineEvent_view, false),
          delBtn: this.vaildData(this.permission.lineEvent_delete, false),
          editBtn: this.vaildData(this.permission.lineEvent_edit, false)
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {
      //打开地图图层
        openGrid(){
        this.mapBox = true
      },

      //地图选择组件回调处理数据
      getGridMap(data){
        console.log("callback:",data)

        if(data){
          this.form.lng = data.lng
          this.form.lat = data.lat
        }

        this.mapBox = false
      },
      rowSave(row, done, loading) {
        add(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          window.console.log(error);
        });
      },
      rowUpdate(row, index, done, loading) {
        update(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          window.console.log(error);
        });
      },
      rowDel(row) {
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(row.id);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      handleDelete() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(this.ids);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection();
          });
      },
      beforeOpen(done, type) {
        //新增和修改页面，控制网格树加载职能选择末级，查询页面可以选择父级
        if (["edit", "add"].includes(type)) {
            const gridId = this.findObject(this.option.column,"gridId");
            gridId.parent = false;
           
        }
        if (["edit", "view"].includes(type)) {
          getDetail(this.form.id).then(res => {
            this.form = res.data.data;
          });
        }
        done();
      },
      searchReset() {
        this.query = {};
        this.onLoad(this.page);
      },
      searchChange(params, done) {
        this.query = params;
        this.page.currentPage = 1
        this.onLoad(this.page, params);
        done();
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      selectionClear() {
        this.selectionList = [];
        this.$refs.crud.toggleSelection();
      },
      currentChange(currentPage){
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize){
        this.page.pageSize = pageSize;
      },
      refreshChange() {
        this.onLoad(this.page, this.query);
      },
      onLoad(page, params = {}) {
        this.loading = true;
        getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.loading = false;
          this.selectionClear();
        });
      }
    }
  };
</script>

<style>
.el-form-item__content{
   margin-left: 160px ! important;
}
.el-form-item__label {
  width: 160px ! important;
}
</style>
